{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Number of Laser Beams in a Bank"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Medium"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #math #string #matrix"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #数学 #字符串 #矩阵"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: numberOfBeams"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #银行中的激光束数量"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>银行内部的防盗安全装置已经激活。给你一个下标从 <strong>0</strong> 开始的二进制字符串数组 <code>bank</code> ，表示银行的平面图，这是一个大小为 <code>m x n</code> 的二维矩阵。 <code>bank[i]</code> 表示第 <code>i</code> 行的设备分布，由若干 <code>'0'</code> 和若干 <code>'1'</code> 组成。<code>'0'</code> 表示单元格是空的，而 <code>'1'</code> 表示单元格有一个安全设备。</p>\n",
    "\n",
    "<p>对任意两个安全设备而言，<strong>如果</strong><strong>同时</strong> 满足下面两个条件，则二者之间存在 <strong>一个</strong> 激光束：</p>\n",
    "\n",
    "<ul>\n",
    "\t<li>两个设备位于两个 <strong>不同行</strong> ：<code>r<sub>1</sub></code> 和 <code>r<sub>2</sub></code> ，其中 <code>r<sub>1</sub> &lt; r<sub>2</sub></code> 。</li>\n",
    "\t<li>满足&nbsp;<code>r<sub>1</sub> &lt; i &lt; r<sub>2</sub></code>&nbsp;的 <strong>所有&nbsp;</strong>行&nbsp;<code>i</code>&nbsp;，都&nbsp;<strong>没有安全设备</strong> 。</li>\n",
    "</ul>\n",
    "\n",
    "<p>激光束是独立的，也就是说，一个激光束既不会干扰另一个激光束，也不会与另一个激光束合并成一束。</p>\n",
    "\n",
    "<p>返回银行中激光束的总数量。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<p><img alt=\"\" src=\"https://assets.leetcode.com/uploads/2021/12/24/laser1.jpg\" style=\"width: 400px; height: 368px;\" /></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>bank = [\"011001\",\"000000\",\"010100\",\"001000\"]\n",
    "<strong>输出：</strong>8\n",
    "<strong>解释：</strong>在下面每组设备对之间，存在一条激光束。总共是 8 条激光束：\n",
    " * bank[0][1] -- bank[2][1]\n",
    " * bank[0][1] -- bank[2][3]\n",
    " * bank[0][2] -- bank[2][1]\n",
    " * bank[0][2] -- bank[2][3]\n",
    " * bank[0][5] -- bank[2][1]\n",
    " * bank[0][5] -- bank[2][3]\n",
    " * bank[2][1] -- bank[3][2]\n",
    " * bank[2][3] -- bank[3][2]\n",
    "注意，第 0 行和第 3 行上的设备之间不存在激光束。\n",
    "这是因为第 2 行存在安全设备，这不满足第 2 个条件。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<p><img alt=\"\" src=\"https://assets.leetcode.com/uploads/2021/12/24/laser2.jpg\" style=\"width: 244px; height: 325px;\" /></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>bank = [\"000\",\"111\",\"000\"]\n",
    "<strong>输出：</strong>0\n",
    "<strong>解释：</strong>不存在两个位于不同行的设备\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>m == bank.length</code></li>\n",
    "\t<li><code>n == bank[i].length</code></li>\n",
    "\t<li><code>1 &lt;= m, n &lt;= 500</code></li>\n",
    "\t<li><code>bank[i][j]</code> 为 <code>'0'</code> 或 <code>'1'</code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [number-of-laser-beams-in-a-bank](https://leetcode.cn/problems/number-of-laser-beams-in-a-bank/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [number-of-laser-beams-in-a-bank](https://leetcode.cn/problems/number-of-laser-beams-in-a-bank/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[\"011001\",\"000000\",\"010100\",\"001000\"]', '[\"000\",\"111\",\"000\"]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfBeams(self, bank: List[str]) -> int:\n",
    "        for i in range(len(bank)):\n",
    "            bank[i] = sum(int(j) for j in list(bank[i]))\n",
    "        ret = cur = 0\n",
    "        for i in range(len(bank)):\n",
    "            if bank[i]:\n",
    "                ret += cur * bank[i]\n",
    "                cur = bank[i]\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfBeams(self, bank: List[str]) -> int:\n",
    "        m = len(bank)\n",
    "\n",
    "        if m == 1:\n",
    "            return 0\n",
    "        \n",
    "        res = 0\n",
    "        device_flag = [0 for i in range(m)]\n",
    "\n",
    "        for i in range(m):\n",
    "            device_flag[i] = bank[i].count('1')\n",
    "        \n",
    "        for i in range(m):\n",
    "            try:\n",
    "                device_flag.remove(0)\n",
    "            except:\n",
    "                break\n",
    "\n",
    "        for i in range(1,len(device_flag)):\n",
    "            res += device_flag[i-1]*device_flag[i]\n",
    "\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfBeams(self, bank: List[str]) -> int:\n",
    "        return sum(map(prod, pairwise(c for s in bank if (c := s.count('1')))))\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfBeams(self, bank: List[str]) -> int:\n",
    "        def NumOfOne(s: str) -> int:\n",
    "            count = 0\n",
    "            l = len(s)\n",
    "            for i in range(l):\n",
    "                count += int(s[i])\n",
    "            return count\n",
    "        temp1 = temp2 = 0\n",
    "        ans = 0\n",
    "        for string in bank:\n",
    "            temp2 = NumOfOne(string)\n",
    "            if temp2 > 0 :\n",
    "                if temp1 > 0:\n",
    "                    ans += temp2*temp1\n",
    "                temp1 = temp2\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfBeams(self, bank: List[str]) -> int:\n",
    "        ans = 0\n",
    "        pre = 0\n",
    "        for b in bank:\n",
    "            cur = b.count(\"1\")\n",
    "            if cur > 0:\n",
    "                ans += pre * cur\n",
    "                pre = cur\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfBeams(self, bank: List[str]) -> int:\n",
    "        num_list=[]\n",
    "        for line in bank:\n",
    "            count=0\n",
    "            for x in line:\n",
    "                if x =='1':\n",
    "                    count+=1\n",
    "            if count>0:\n",
    "                num_list.append(count)\n",
    "        count=0\n",
    "        while len(num_list)>1:\n",
    "            pre=num_list.pop()\n",
    "            las=num_list[-1]\n",
    "\n",
    "            count+=pre*las\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfBeams(self, bank: List[str]) -> int:\n",
    "        ans = 0\n",
    "        n = len(bank)\n",
    "        m = len(bank[0])\n",
    "        pre = 0\n",
    "        for i in range(n):\n",
    "            temp = 0\n",
    "            for j in bank[i]:\n",
    "                if j == '1':\n",
    "                    temp += 1\n",
    "            ans += pre * temp\n",
    "            if temp > 0:\n",
    "                pre = temp\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfBeams(self, bank: List[str]) -> int:\n",
    "        l = len(bank)\n",
    "        b = 0\n",
    "        ans = 0\n",
    "        i = 0\n",
    "        while i < l:\n",
    "            while i < l and bank[i].count('1') == 0:\n",
    "                i += 1\n",
    "            if i < l:\n",
    "                n = bank[i].count('1')\n",
    "                ans += n * b\n",
    "                b = n\n",
    "            i += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfBeams(self, bank: List[str]) -> int:\n",
    "        m = len(bank)\n",
    "        st_row = ans = 0\n",
    "        cnt_st = 0\n",
    "\n",
    "        while st_row < m:\n",
    "            while cnt_st == 0 and st_row < m:\n",
    "                if '1' in bank[st_row]:\n",
    "                    for c in bank[st_row]:\n",
    "                        cnt_st += int(c)\n",
    "                st_row += 1\n",
    "\n",
    "            if cnt_st !=0:\n",
    "                end_row = st_row\n",
    "                cnt_end = 0\n",
    "\n",
    "                while cnt_end == 0 and end_row < m:\n",
    "                    if '1' in bank[end_row]:\n",
    "                        for c in bank[end_row]:\n",
    "                            cnt_end += int(c)\n",
    "                    end_row += 1\n",
    "\n",
    "                ans += (cnt_st * cnt_end)\n",
    "                cnt_st = cnt_end\n",
    "                st_row = end_row - 1\n",
    "\n",
    "            st_row += 1\n",
    "                \n",
    "        return ans\n",
    "            \n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfBeams(self, bank: List[str]) -> int:\n",
    "        nums = [n for n in [sum([1 for s in ss if s == '1']) for ss in bank] if n > 0]\n",
    "        return sum([nums[i] * nums[i + 1] for i in range(len(nums) - 1)])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfBeams(self, bank: List[str]) -> int:\n",
    "        ans, pre = 0, ''\n",
    "        rowLens, colLens = len(bank), len(bank[0])\n",
    "        for r in range(rowLens):\n",
    "            c = Counter(bank[r])\n",
    "            if c['1'] > 0: # 当前有激光\n",
    "                if pre:\n",
    "                    ans += c['1'] * Counter(pre)['1']\n",
    "                pre = bank[r]\n",
    "        return ans\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfBeams(self, bank: List[str]) -> int:\n",
    "        m = len(bank)\n",
    "        st_row = ans = 0\n",
    "        cnt_st = 0\n",
    "        while st_row < m:\n",
    "            while cnt_st == 0 and st_row < m:\n",
    "                cnt_st = Counter(bank[st_row])['1']\n",
    "                st_row += 1\n",
    "\n",
    "            if cnt_st !=0:\n",
    "                end_row = st_row\n",
    "                cnt_end = 0\n",
    "                \n",
    "                while cnt_end == 0 and end_row < m:\n",
    "                    cnt_end = Counter(bank[end_row])['1']\n",
    "                    end_row += 1\n",
    "\n",
    "                ans += (cnt_st * cnt_end)\n",
    "                cnt_st = cnt_end\n",
    "                st_row = end_row - 1\n",
    "\n",
    "            st_row += 1\n",
    "                \n",
    "        return ans\n",
    "            \n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfBeams(self, bank: List[str]) -> int:\n",
    "        m=len(bank)\n",
    "        n=len(bank[0])\n",
    "        a=[]\n",
    "        for j in range(m):\n",
    "            sums=0\n",
    "            for i in range(n):\n",
    "                sums+=int(bank[j][i])\n",
    "            a.append(sums)\n",
    "        while 0 in a:\n",
    "            a.remove(0)\n",
    "        ans=0\n",
    "        for i in range(len(a)-1):\n",
    "            ans+=a[i]*a[i+1]\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfBeams(self, bank: List[str]) -> int:\n",
    "        l = [c for s in bank if (c := s.count('1'))]\n",
    "        return sum(map(prod, pairwise(l)))\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfBeams(self, bank: List[str]) -> int:\n",
    "        fh = 0\n",
    "        x = sum(i == \"1\" for i in bank[0])\n",
    "        for i in range(1, len(bank)):\n",
    "            y = sum(j==\"1\" for j in bank[i])\n",
    "            if y:\n",
    "                fh += x * y\n",
    "                x = y\n",
    "        return fh "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfBeams(self, bank: List[str]) -> int:\n",
    "        m = len(bank)\n",
    "        st_row = ans = 0\n",
    "        cnt_st = 0\n",
    "\n",
    "        while st_row < m:\n",
    "            while cnt_st == 0 and st_row < m:\n",
    "                if '1' in bank[st_row]:\n",
    "                    cnt_st = Counter(bank[st_row])['1']\n",
    "                st_row += 1\n",
    "\n",
    "            if cnt_st !=0:\n",
    "                end_row = st_row\n",
    "                cnt_end = 0\n",
    "\n",
    "                while cnt_end == 0 and end_row < m:\n",
    "                    if '1' in bank[end_row]:\n",
    "                        cnt_end = Counter(bank[end_row])['1']\n",
    "                    end_row += 1\n",
    "\n",
    "                ans += (cnt_st * cnt_end)\n",
    "                cnt_st = cnt_end\n",
    "                st_row = end_row - 1\n",
    "\n",
    "            st_row += 1\n",
    "                \n",
    "        return ans\n",
    "            \n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfBeams(self, bank: List[str]) -> int:\n",
    "        ans = pre = 0\n",
    "        for x in bank:\n",
    "            t = x.count('1')\n",
    "            ans += t*pre\n",
    "            if t:\n",
    "                pre = t\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfBeams(self, bank: List[str]) -> int:\n",
    "        a=[]\n",
    "        for i in bank:\n",
    "            t=i.count('1')\n",
    "            if t!=0:a.append(t)\n",
    "        b=0\n",
    "        for i in range(len(a)-1):\n",
    "            b+=a[i]*a[i+1]\n",
    "        return b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfBeams(self, bank: List[str]) -> int:\n",
    "        count = [i.count('1') for i in bank if i.count('1') != 0]\n",
    "        res = 0\n",
    "        if len(count) <2:\n",
    "            return res\n",
    "        else:\n",
    "            for i in range(len(count)-1):\n",
    "                res +=count[i]*count[i+1]\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfBeams(self, bank: List[str]) -> int:\n",
    "        last, ans = 0, 0\n",
    "        for line in bank:\n",
    "            cnt = line.count('1')\n",
    "            if cnt != 0:\n",
    "                ans += cnt * last\n",
    "                last = cnt\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfBeams(self, bank: List[str]) -> int:\n",
    "        ans = 0\n",
    "        pre = 0\n",
    "\n",
    "        for device in bank:\n",
    "            count = 0\n",
    "            for ele in device:\n",
    "                if ele == '1':\n",
    "                    count += 1\n",
    "            if count == 0:\n",
    "                continue\n",
    "            else:\n",
    "                ans += pre * count\n",
    "            pre = count\n",
    "        \n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfBeams(self, bank: List[str]) -> int:\n",
    "        l = []\n",
    "        for s in bank:\n",
    "            c = s.count('1')\n",
    "            if c > 0:\n",
    "                l.append(c)\n",
    "        return sum(map(prod, pairwise(l)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfBeams(self, bank: List[str]) -> int:\n",
    "        m = len(bank)\n",
    "        st_row = ans = 0\n",
    "        cnt_st = 0\n",
    "        while st_row < m:\n",
    "            while cnt_st == 0 and st_row < m:\n",
    "                for c in bank[st_row]:\n",
    "                    cnt_st += int(c == '1')\n",
    "                st_row += 1\n",
    "            if cnt_st !=0:\n",
    "                end_row = st_row\n",
    "                cnt_end = 0\n",
    "                while cnt_end == 0 and end_row < m:\n",
    "                    for c in bank[end_row]:\n",
    "                        cnt_end += int(c == '1')\n",
    "                    end_row += 1\n",
    "                ans += (cnt_st * cnt_end)\n",
    "                cnt_st = cnt_end\n",
    "                st_row = end_row - 1\n",
    "            st_row += 1\n",
    "                \n",
    "        return ans\n",
    "            \n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfBeams(self, bank: List[str]) -> int:\n",
    "        r1 = 0\n",
    "        r2 = 1\n",
    "        total_row = len(bank)\n",
    "        result = 0\n",
    "        while r1 <= total_row - 2:\n",
    "            r2 = r1 + 1\n",
    "            middle = False\n",
    "            while r2 <= total_row - 1:\n",
    "                if middle == False:\n",
    "                    if '1' in bank[r2]:\n",
    "                        result += bank[r1].count(\"1\") * bank[r2].count(\"1\")\n",
    "                        middle = True\n",
    "                \n",
    "                r2 += 1 \n",
    "            r1 += 1\n",
    "        return result\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfBeams(self, bank: List[str]) -> int:\n",
    "        fh = 0\n",
    "        x = sum(i == \"1\" for i in bank[0])\n",
    "        for i in range(1, len(bank)):\n",
    "            y = sum(j==\"1\" for j in bank[i])\n",
    "            if y:\n",
    "                fh += x * y\n",
    "                x = y\n",
    "        return fh "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfBeams(self, bank: List[str]) -> int:\n",
    "        def NumOfOne(s: str) -> int:\n",
    "            return s.count(\"1\")\n",
    "        temp1 = temp2 = 0\n",
    "        ans = 0\n",
    "        for string in bank:\n",
    "            temp2 = NumOfOne(string)\n",
    "            if temp2 > 0 :\n",
    "                if temp1 > 0:\n",
    "                    ans += temp2*temp1\n",
    "                temp1 = temp2\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfBeams(self, bank: List[str]) -> int:\n",
    "        res = 0\n",
    "        prev = 0\n",
    "        for i in bank:\n",
    "            tmp_num = i.count('1')\n",
    "            if tmp_num == 0 :\n",
    "                continue\n",
    "            else:\n",
    "                res += prev * tmp_num\n",
    "                prev = tmp_num\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfBeams(self, bank: List[str]) -> int:\n",
    "        nums = [c for i in range(len(bank)) if (c := bank[i].count('1'))]\n",
    "        if len(nums) == 0:\n",
    "            return 0\n",
    "        return sum([nums[i] * nums[i+1] for i in range(len(nums)-1)])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfBeams(self, bank: List[str]) -> int:\n",
    "        a = [x.count('1') for x in bank]\n",
    "        a = [x for x in a if x > 0]\n",
    "        a = [a[i-1]*a[i] for i in range(1, len(a))]\n",
    "        return sum(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfBeams(self, bank: List[str]) -> int:\n",
    "        cnt = [c for s in bank if (c:= s.count(\"1\"))]\n",
    "        return sum(cnt[i]*cnt[i+1] for i in range(len(cnt)-1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfBeams(self, bank: List[str]) -> int:\n",
    "        ans = 0\n",
    "        nums = [b.count('1') for b in bank if b.count('1')]\n",
    "        for i in range(1, len(nums), 1):\n",
    "            ans += nums[i] * nums[i-1] \n",
    "        return ans\n",
    "            \n",
    "        \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfBeams(self, bank: List[str]) -> int:\n",
    "        flag=0\n",
    "        bn=0\n",
    "        s=0\n",
    "        for i in range(len(bank)):\n",
    "            cn=self._get(bank[i])\n",
    "            if flag==0 and cn>0:\n",
    "                flag=1\n",
    "                bn=cn\n",
    "            elif flag==1 and cn>0:\n",
    "\n",
    "                s+=cn*bn \n",
    "                bn=cn \n",
    "        return s \n",
    "    \n",
    "    def _get(self,s):\n",
    "        c=0\n",
    "        for i in range(len(s)):\n",
    "            if s[i]=='1':\n",
    "                c+=1\n",
    "        return c "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfBeams(self, bank: List[str]) -> int:\n",
    "        last = ans = 0\n",
    "        for line in bank:\n",
    "            cnt = line.count(\"1\")\n",
    "            if cnt != 0:\n",
    "                ans += last * cnt\n",
    "                last = cnt\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfBeams(self, bank: List[str]) -> int:\n",
    "        cnt = [s.count('1') for s in bank if s.count('1')]\n",
    "        return sum(cnt[i] * cnt[i + 1] for i in range(len(cnt) - 1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfBeams(self, bank: List[str]) -> int:\n",
    "        num = 0\n",
    "        last_cnt = 0\n",
    "\n",
    "        for sub_line in bank:\n",
    "            sub_cnt = 0\n",
    "            for item in sub_line:\n",
    "                if item == \"1\":\n",
    "                    sub_cnt+=1\n",
    "\n",
    "            if sub_cnt == 0:\n",
    "                continue\n",
    "\n",
    "            if last_cnt == 0:\n",
    "                last_cnt = sub_cnt\n",
    "            else:\n",
    "                num += last_cnt * sub_cnt\n",
    "                last_cnt = sub_cnt\n",
    "        return num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfBeams(self, bank: List[str]) -> int:\n",
    "        last_layer=0\n",
    "        result=0\n",
    "        for i in bank:\n",
    "            beam_num=sum([1 for c in i if c=='1'])\n",
    "            result+=last_layer*beam_num\n",
    "            if beam_num!=0:\n",
    "                last_layer=beam_num\n",
    "        return result\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfBeams(self, bank: List[str]) -> int:\n",
    "        last_layer=0\n",
    "        result=0\n",
    "        for i in bank:\n",
    "            beam_num=sum([1 for c in i if c=='1'])\n",
    "            result+=last_layer*beam_num\n",
    "            if beam_num!=0:\n",
    "                last_layer=beam_num\n",
    "        return result\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfBeams(self, bank: List[str]) -> int:\n",
    "        res = 0\n",
    "        temp = -1\n",
    "        for i in bank:\n",
    "            n = i.count('1')\n",
    "            if n == 0:\n",
    "                continue\n",
    "            if temp != -1:\n",
    "                res += temp * n\n",
    "            temp = n\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfBeams(self, bank: List[str]) -> int:\n",
    "        last = ans = 0\n",
    "        for line in bank:\n",
    "            cnt = line.count(\"1\")\n",
    "            if cnt != 0:\n",
    "                ans += last * cnt\n",
    "                last = cnt\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfBeams(self, bank: List[str]) -> int:\n",
    "        cnt = [c for s in bank if (c:= s.count(\"1\"))]\n",
    "        return sum(cnt[i]*cnt[i+1] for i in range(len(cnt)-1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfBeams(self, bank: List[str]) -> int:\n",
    "        ans=0\n",
    "        pre=0\n",
    "        for i,t in enumerate(bank):\n",
    "            ct=t.count('1')\n",
    "            if ct==0:\n",
    "                continue\n",
    "            else:\n",
    "                if pre==0:\n",
    "                    pre=ct\n",
    "                    continue\n",
    "                else:\n",
    "                    ans+=pre*ct\n",
    "                    pre=ct\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfBeams(self, bank: List[str]) -> int:\n",
    "        s = [x.count('1') for x in bank if x.count('1') > 0]\n",
    "        return sum(a * b for a, b in pairwise(s))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfBeams(self, bank: List[str]) -> int:\n",
    "        m = len(bank)\n",
    "        n = len(bank[0])\n",
    "        pre = 0\n",
    "        ret = 0\n",
    "        for b in bank:\n",
    "            now = sum(int(x) for x in b)\n",
    "            if now:\n",
    "                ret += now * pre\n",
    "                pre = now\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfBeams(self, bank: List[str]) -> int:\n",
    "        last=ans=0\n",
    "        for line in bank:\n",
    "            cnt=line.count(\"1\")\n",
    "            if cnt!=0:\n",
    "                ans+=last*cnt\n",
    "                last=cnt\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfBeams(self, bank: List[str]) -> int:\n",
    "        flag=0\n",
    "        bn=0\n",
    "        s=0\n",
    "        for i in range(len(bank)):\n",
    "            cn=self._get(bank[i])\n",
    "            if flag==0 and cn>0:\n",
    "                flag=1\n",
    "                bn=cn\n",
    "            elif flag==1 and cn>0:\n",
    "\n",
    "                s+=cn*bn \n",
    "                bn=cn \n",
    "        return s \n",
    "    \n",
    "    def _get(self,s):\n",
    "        c=0\n",
    "        for i in range(len(s)):\n",
    "            if s[i]=='1':\n",
    "                c+=1\n",
    "        return c "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfBeams(self, bank: List[str]) -> int:\n",
    "        arr = []\n",
    "        for i in bank:\n",
    "            c = i.count('1')\n",
    "            if c != 0:\n",
    "                arr.append(c)\n",
    "        if len(arr) <= 1: return 0\n",
    "        ans = 0\n",
    "        for i in range(len(arr) - 1):\n",
    "            ans += arr[i] * arr[i + 1]\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfBeams(self, bank: List[str]) -> int:\n",
    "        ret=0\n",
    "        last=0\n",
    "        for s in bank:\n",
    "            now=s.count(\"1\")\n",
    "            if now==0:\n",
    "                continue\n",
    "            ret+=last*now \n",
    "            last=now \n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfBeams(self, bank: List[str]) -> int:\n",
    "      ans , data = 0 , []\n",
    "      for i in bank:\n",
    "        num = i.count('1')\n",
    "        if num != 0:\n",
    "          data.append(num)\n",
    "      if len(data) == 1:\n",
    "        return 0\n",
    "      for i in range(1,len(data)):\n",
    "        ans = ans + data[i]*data[i-1]\n",
    "      return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfBeams(self, bank: List[str]) -> int:\n",
    "        ans=a=b=0\n",
    "        for i in bank:\n",
    "            if '1' in i:\n",
    "                if a>0:\n",
    "                    b=i.count('1')\n",
    "                    ans+=a*b\n",
    "                    a=b\n",
    "                else:\n",
    "                    a=i.count(\"1\")\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfBeams(self, bank: List[str]) -> int:\n",
    "        s = []\n",
    "        for b in bank:\n",
    "            c = b.count(\"1\")\n",
    "            if c:\n",
    "                s.append(c)\n",
    "        val = 0\n",
    "        if len(s) <= 1:\n",
    "            return 0\n",
    "        for ix in range(1, len(s)):\n",
    "            val += s[ix] * s[ix - 1]\n",
    "        return val"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfBeams(self, bank: List[str]) -> int:\n",
    "        ans = 0\n",
    "        cnt = [s.count('1') for s in bank if s.count('1')]\n",
    "        for i in range(len(cnt) - 1):\n",
    "            ans += cnt[i] * cnt[i + 1]\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfBeams(self, bank: List[str]) -> int:\n",
    "        n=len(bank)\n",
    "        label=[]\n",
    "        for i in range(n):\n",
    "            io=bank[i].count('1')\n",
    "            if io!=0:\n",
    "                label.append(io)\n",
    "        result=0\n",
    "        for i in range(1, len(label)):\n",
    "            result+=label[i-1]*label[i]\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfBeams(self, bank: List[str]) -> int:\n",
    "        last_layer=0\n",
    "        result=0\n",
    "        for i in bank:\n",
    "            beam_num=sum([1 for c in i if c=='1'])\n",
    "            result+=last_layer*beam_num\n",
    "            if beam_num!=0:\n",
    "                last_layer=beam_num\n",
    "        return result\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfBeams(self, bank: List[str]) -> int:\n",
    "        last = ans = 0\n",
    "        for line in bank:\n",
    "            cnt = line.count(\"1\")\n",
    "            if cnt != 0:\n",
    "                ans += last * cnt\n",
    "                last = cnt\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfBeams(self, bank: List[str]) -> int:\n",
    "        bank_equ = []\n",
    "        for i in range(len(bank)):\n",
    "            if \"1\" in bank[i]:\n",
    "                bank_num =list(map(int, bank[i]))\n",
    "                bank_equ.append(sum(bank_num))\n",
    "        ans = 0\n",
    "        if len(bank_equ) == 1:\n",
    "            return ans\n",
    "        for i in range(len(bank_equ)-1):\n",
    "            ans += bank_equ[i]*bank_equ[i+1]\n",
    "        return ans\n",
    "            \n",
    "      \n",
    " \n",
    "\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfBeams(self, bank: List[str]) -> int:\n",
    "        cnt = [c for s in bank if (c:= s.count(\"1\"))]\n",
    "        return sum(cnt[i]*cnt[i+1] for i in range(len(cnt)-1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numberOfBeams(self, bank: List[str]) -> int:\n",
    "        count1=0\n",
    "        count2=0\n",
    "        temp=0\n",
    "        ans=0\n",
    "        for cci in bank:\n",
    "            for i in cci:\n",
    "                if int(i)==1:\n",
    "                    temp+=1\n",
    "            if temp!=0:\n",
    "                if count2!=0:\n",
    "                    count1,count2,temp=count2,temp,0\n",
    "                    ans+=count1*count2\n",
    "                elif count1!=0:\n",
    "                    count2,temp=temp,0\n",
    "                    ans+=count1*count2\n",
    "                else:\n",
    "                    count1,temp=temp,0\n",
    "        return ans"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
